<?php

namespace app\admin\model;

use think\Model;


class Zuzhi extends Model
{

    

    

    // 表名
    protected $name = 'zuzhi';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];

    public function selectMenus_res($res) {
        if(count($res) == 1){
            $tmpArr = $this->nodeTrees($res,$res[0]['pid']);
        }else{
            $tmpArr = $this->nodeTrees($res,$res[0]['pid']);
        }
        $data = array();
        // $data[0] = '请选择组织';
        foreach ($tmpArr as $k => $v) {
            $name = $v['level'] == 0 ? $v['name'] : '├─' . $v['name'];
//            $name = $v['level'] == 0 ? '<b>' . $v['name'] . '</b>' : '├─' . $v['name'];
            $name = str_repeat("│　", $v['level']) . $name;
            $data[$k]['id'] = $v['id'];
            $data[$k]['name'] = $name;
        }
        return $data;
    }
    public function nodeTrees($arr, $id = 0, $level = 0) {
        // dump($arr);die;
        static $array = array();
        foreach ($arr as $v) {
            if ($v['pid'] == $id) {
                $v['level'] = $level;
                $array[] = $v;
                $this->nodeTrees($arr, $v['id'], $level + 1);
            }
        }
        // dump($array);die;
        return $array;
    }

    public function selectMenus($cateid) {
        $res = db('zuzhi')
            ->where(['delete_id' => 0])
            ->field('id,name,pid,jigou')
            ->select();
        $tmpArr = $this->nodeTrees($res,$cateid,1);
        $res_find = db('zuzhi')
            ->where('id',$cateid)
            ->field('id,name,pid,jigou')
            ->find();
        $res_find['level'] = 0;
        array_unshift($tmpArr,$res_find);
        $data = array();
//        $data[0] = '请选择组织';
        foreach ($tmpArr as $k => $v) {
            $name = $v['level'] == 0 ? '<b>' . $v['name'] . '</b>' : '├─' . $v['name'];

            $name = str_repeat("│　", $v['level']) . $name;
            $data[$v['id']] = $name;
        }
        return $data;
    }
    /**
     * 择选栏目
     */
    public function selectMenu() {
        $res = db('zuzhi')
            ->where(['delete_id' => 0])
            ->field('id,name,pid,jigou')
            ->select();
        $tmpArr = $this->nodeTrees($res);
        $data = array();
        $data[] = '请选择归属部门';
        foreach ($tmpArr as $k => $v) {
            $name = $v['level'] == 0 ? $v['name'] : '├─' . $v['name'];

            $name = str_repeat("│　", $v['level']) . $name;
            $data[$v['id']] = $name;
        }
        //  dump($data);
        // exit;
        return $data;
    }

    







}
